package crushbum.gamelevels;
import box2D.collision.B2AABB;
import box2D.common.math.B2Vec2;
import box2D.dynamics.B2DebugDraw;
import box2D.dynamics.B2World;
import com.funkyfurrymonkey.load.AssetsRepository;
import com.funkyfurrymonkey.load.Preloader;
import com.funkyfurrymonkey.world2d.StaticActor;
import crushbum.constants.Kte;
import crushbum.DataController;
import crushbum.elements.GrassHillLeft;
import openfl.display.Sprite;
import openfl.events.Event;

/**
 * ...
 * @author ...
 */
class GameLevel
{
	private var m_cCanvas:Sprite;
	private var m_sLevelName:String;
	private var m_cData:DataController;
	private var m_cRepo:AssetsRepository;
	var cPreloader:Preloader ;
	
	private var m_cGravity:B2Vec2;
	private var m_cWorld:B2World;
	private var m_cPhysicsDebug:Sprite;
	
	public function new(cData:DataController) 
	{		
		m_cCanvas = new Sprite();
		
		m_cRepo = new AssetsRepository();
		m_cData = cData;
		m_cData.m_cMainCanvas.addChild(m_cCanvas);		
		
		//setting world2d
		setupWorld2d();
	
	}
	
	function setupWorld2d() 
	{
		// set up the size of the universe
		var vUniverserSize:B2AABB = new B2AABB();
		vUniverserSize.lowerBound.set( -3000 / Kte.RATIO, -3000 / Kte.RATIO);
		vUniverserSize.upperBound.set(3000 / Kte.RATIO, 3000 / Kte.RATIO);// i want a 6000x6000 pixels box!!!! box box2d works in meters
		
		//defines gravity
		m_cGravity = new B2Vec2(0, 9.8);
		
		
		//ignore sleesping 
		var bIgnoreSleeping:Bool = true;
		
		m_cWorld = new B2World(m_cGravity, bIgnoreSleeping);
		m_cWorld.
		
		m_cPhysicsDebug = new Sprite();
		setDebugDraw();
		
	}
	
	public function create(nLevel:Int)
	{
		m_sLevelName = "level" + nLevel;
		cPreloader = new Preloader(m_cCanvas, m_cRepo );
		cPreloader.onComplete = preloadComplete;
		cPreloader.start("assets/"+m_sLevelName+".xml");
	}
	
	private function preloadComplete() 
	{
		
		cPreloader.removePreloaderScreen();
		//var cGround:StaticActor = new StaticActor(m_cWorld);
		//cGround.setBitmap(m_cRepo.getImage("tile_gh"), Kte.RATIO);
		//m_cCanvas.addChild(cGround.m_cCanvas);
		//cGround.setPosition(100,430);
		
		m_cData.m_cMainCanvas.stage.addEventListener(Event.ENTER_FRAME, movingWorld);
		
		//while (m_cCanvas.numChildren > 0)
		//	m_cCanvas.removeChildAt(0);
		m_cCanvas.addChild(m_cPhysicsDebug);// TODO :	Get ride of phusics at end , it sjut for debugging
		createElements();
		
		ready();
	}
	
	function createElements() 
	{
		var e:GrassHillLeft = new GrassHillLeft(m_cRepo.getImage("grasshillleft"));
		m_cCanvas.addChild(e.m_cCostume);
	}
	
	dynamic public function ready(){};
	private function movingWorld(ev:Event):Void
	{
		m_cWorld.step(0.025 , 10, 10);
		m_cWorld.drawDebugData();
		m_cWorld.clearForces();
	}
	
		private function setDebugDraw():Void
	{
				// set debug draw
		var dbgDraw:B2DebugDraw = new B2DebugDraw();

		
		dbgDraw.setSprite(m_cCanvas);
		dbgDraw.setDrawScale(1 *3);
		dbgDraw.setFillAlpha(0.3);
		dbgDraw.setLineThickness(1.0);
		dbgDraw.setFlags(B2DebugDraw.e_shapeBit | B2DebugDraw.e_jointBit);
		
		
		m_cWorld.setDebugDraw(dbgDraw);
	}
}